################################################################################
# Judicial Ideology as Text -- Figure A6: Comparison of Static v. Dynamic Means (High)
# Truscott & Romano
# Updated February 2025
################################################################################


################################################################################
# Load Source
################################################################################

source('R/truscott_romano_source.R') # Load Source

################################################################################
# Figure A6: Comparison of Dynamic v. Static Means
################################################################################


{

  static_mean <- wordshoals_static$High$summary_output %>%
    mutate(static_theta = theta)
  dynamic_mean <- data.frame()
  for (term in 1:length(wordshoals_dynamic$High)){
    dynamic_mean <- bind_rows(dynamic_mean, wordshoals_dynamic$High[[term]]$summary_output %>%
                                mutate(term = names(wordshoals_dynamic$High)[term]))
  }
  dynamic_mean <- dynamic_mean %>%
    group_by(author) %>%
    summarise(dynamic_mean_theta = mean(theta))

  combined_static_dynamic <- static_mean %>%
    left_join(dynamic_mean, by = 'author')

  correlation <- combined_static_dynamic %>%
    mutate(
      static_theta = abs(theta - mean(theta)) / sd(theta),
      dynamic_theta = abs(dynamic_mean_theta - mean(dynamic_mean_theta)) / sd(dynamic_mean_theta),
      ideology = ifelse(author %in% c('Ginsburg', 'Sotomayor', 'Breyer', 'Kagan'), 'Democratic Appointee', 'Republican Appointee')) %>%
    dplyr::select(author, static_theta, dynamic_theta, ideology)

  correlation <- cor(correlation$static_theta, correlation$dynamic_theta, method = 'pearson')

  Figure_A6 <- combined_static_dynamic %>%
    mutate(
      static_theta = abs(theta - mean(theta)) / sd(theta),
      dynamic_theta = abs(dynamic_mean_theta - mean(dynamic_mean_theta)) / sd(dynamic_mean_theta),
      ideology = ifelse(author %in% c('Ginsburg', 'Sotomayor', 'Breyer', 'Kagan'), 'Democratic Appointee', 'Republican Appointee')) %>%
    dplyr::select(author, static_theta, dynamic_theta, ideology) %>%
    ggplot(aes(x = static_theta, y = dynamic_theta)) +
    geom_segment(x = 0, xend = 1.75, y = 0, yend = 1.75, size = 1.25) +
    geom_point(aes(fill = ideology), colour = 'black', shape = 21, size = 3) +
    scale_fill_manual(values = c('deepskyblue3', 'red3')) +
    labs(x = '\nStatic Wordshoal (Scaled)',
         y = 'Dynamic Wordshoal (Scaled)\n') +
    scale_y_continuous(lim = c(0, 1.75), breaks = seq(0, 1.5, 0.5)) +
    scale_x_continuous(lim = c(0, 1.75), breaks = seq(0, 1.5, 0.5)) +
    ggrepel::geom_label_repel(aes(label = author), label.padding = 0.25) +
    theme_minimal() +
    theme(panel.border = element_rect(size = 1, colour = 'black', fill = NA),
          axis.text = element_text(size = 12, colour = 'black'),
          axis.title = element_text(size = 14, colour = 'black'),
          legend.position = 'bottom',
          legend.text = element_text(size = 14, colour = 'black'),
          legend.box.background = element_rect(size = 1, colour = 'black', fill = NA),
          legend.title = element_blank()) +
    ggplot2::annotate('label', x = 0.15, y = 1.5, label = 'More Ideological\n(Dynamic)', size = 5) +
    ggplot2::annotate('label', x = 1.5, y = 0.1, label = 'More Ideological\n(Static)', size = 5)



  ggsave(Figure_A6,
         file = 'figures/Figure_A6.png',
         width = 10,
         height = 10,
         units = 'in',
         bg = 'white')

  ggsave('figures_tiff/Figure_A6.tiff',
         plot = Figure_A6,
         device = 'tiff',
         dpi = 600,
         width = 10,
         height = 10,
         units = 'in',
         bg = 'white')


  rm(static_mean, dynamic_mean, combined_static_dynamic, term)


  } # Figure A6: Comparison of Dynamic v. Static Means (High Weight) x Justice


